<?php
require_once('Extension/Controller/Helper/Abstract.php');

class Extension_Controller_Helper_Web_Form extends Extension_Controller_Helper_Abstract
{
    public function init()
    {
    }

    public function preDispatch()
    {
    }

    public function postDispatch()
    {
    }

    public function getFormLogin()
    {
        $form = new Extension_Form();
        $view = $form->getView();
        $form->setAction($view->U('user', 'login'));
        $form->setMethod(Zend_Form::METHOD_POST);
        $form->setName('formLogin');
        $form->addElementPrefixPath('Extension_Form_Decorator', 'Extension/Form/Decorator', Zend_Form_Element::DECORATOR);
        $form->addElementPrefixPath('Extension_Form_Filter', 'Extension/Form/Filter', Zend_Form_Element::FILTER);
        $form->addElementPrefixPath('Extension_Form_Validate', 'Extension/Form/Validate', Zend_Form_Element::VALIDATE);
        $form->setElementFilters(array('StringTrim'));
        $view->placeholder('body_onload')->set("document.getElementById('formLogin').userLogin.focus();");


        $ajaxUrl = $view->U('user', 'check-login', 'ajax', array('login' => ''));
        $fieldUsername = new Zend_Form_Element_Text('userLogin');
        $fieldUsername->setRequired(true);
        $fieldUsername->setLabel('User Login');
        $fieldUsername->setDescription("* required");
        $fieldUsername->setOptions(array(
            'onkeyup'  => "checkUserLogin('checkUserLoginMessage', '" . $ajaxUrl . "', this.value)",
            'class'    => 'input',
            'tabindex' => '10',
        ));
        $fieldUsername->setAttrib('maxLength', 64);
        $fieldUsername->addDecorators(array(
            array('ViewHelper'),
            array('Errors'),
            array('HtmlTag',     array('tag'    => 'p')),
            array('Label',       array('tag'    => 'dt')),
            array('Description', array('id'     => 'checkUserLoginMessage', 
                                       'escape' => false, 
                                       'tag'    => 'span')),
        ));
        $fieldUsername->addFilter('Login');
        $fieldUsername->addValidators(array(
            array('Login',        true),
            array('NotEmpty',     true),
            array('alnum',        true),
            array('StringLength', false, array(3, 64)),
            array('regex',        false, array('/^[a-z]/i')),
        ));


        $fieldPassword = new Zend_Form_Element_Password('userPass');
        $fieldPassword->setRequired(true);
        $fieldPassword->setOptions(array('class'    => 'password', 'tabindex' => '20',));
        $fieldPassword->setAttrib('maxLength', 64);
        $fieldPassword->setDescription('* required');
        $fieldPassword->setLabel('User Password');
        $fieldPassword->addValidator('StringLength', false, array(3));
        $fieldPassword->addDecorators(array(
            array('ViewHelper'),
            array('Errors'),
            array('HtmlTag',     array('tag'    => 'p')),
            array('Label',       array('tag'    => 'dt')),
            array('Description', array('tag'    => 'span')),
        ));


        $fieldRemember = new Zend_Form_Element_Checkbox('remember');
        //$fieldRemember->setLabel('Remember Me');
        $fieldRemember->setDescription('Remember Me');
        $fieldRemember->setOptions(array('class' => 'options',
                                         'tabindex' => '30',));
        $fieldRemember->addDecorators(array(
            array('ViewHelper'),
            array('Errors'),
            array('HtmlTag',     array('tag'    => 'dt')),
            //array('Label',       array('tag'    => 'dt')),
            array('Description', array('tag'    => 'span')),
        ));


        $fieldToken = new Zend_Form_Element_Hidden('token');
        $fieldToken->setValue(md5($_SERVER['HTTP_USER_AGENT']));

        $fieldReset = new Zend_Form_Element_Reset('reset');
        $fieldReset->setAttrib('style', 'width:100px');
        
        $fieldHash = new Zend_Form_Element_Hash('no_csrf_token');
        $fieldHash->setSalt('unique');
 
        $fieldSubmit = new Zend_Form_Element_Submit('submitted');
        $fieldSubmit->setLabel('Log In');
        $fieldSubmit->setOptions(array('class' => 'submit',
                                       'tabindex' => '100',
                                       'style' => 'width:100px'));


        $fieldUsername->setOrder(1);
        $fieldPassword->setOrder(3);
        $fieldRemember->setOrder(5);
        $fieldToken->setOrder(7);
        $fieldReset->setOrder(9);
        $fieldHash->setOrder(11);
        $fieldSubmit->setOrder(15);

        $form->addElement($fieldUsername);
        $form->addElement($fieldPassword);
        $form->addElement($fieldRemember);
        $form->addElement($fieldToken);
        $form->addElement($fieldReset);
        $form->addElement($fieldHash);
        $form->addElement($fieldSubmit);
        
        $form->setDefaults(array('userLogin' => '', 'userPass' => ''));

        /*
        $form->addDisplayGroup(
            array('userLogin', 'userPass', 'userOptions'), 
            'groupLogin', 
            array('displayGroupClass' => 'Extension_Form_DisplayGroup')
        );
        */

        $form->addDisplayGroupPrefixPath('Extension_Form_Decorator', 'Extension/Form/Decorator/');
        $form->setDisplayGroupDecorators(array(
            'FormElements',
            'Fieldset',
        ));


        /*
        $subForm = new Zend_Form();
        $subForm->addElement('Text', 'subFormTitle', array('value' => 'sub', 'disabled' => 'disabled'));
        $form->addSubForm($subForm, 'subForm');
        $form->setSubFormDecorators(
            array('FormElements', 'Fieldset')
        );
        */


        //var_dump($form->userPass);
        //var_dump($form->getElement('userPass'));
        //var_dump($form->getDisplayGroup('groupLogin'));

        return $form;
    }


}